<!DOCTYPE html>
<html lang="zn-CN">
<head>
    <meta charset="UTF-8">
    <title>个人通讯录</title>
    <link rel="stylesheet" href="static/layui/css/layui.css">
    <script src="static/layui/layui.js"></script>
</head>
<body>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="informationTable" lay-filter="informationTable"></table>
    </div>
</div>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <!-- layui-anim layui-anim-up增加动画效果 -->
        <button class="layui-btn layui-btn-sm layui-anim layui-anim-down" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm layui-anim layui-anim-down" lay-event="batchDelete">批量删除</button>
    </div>
</script>

<!-- 公共表单模块 -->
<form class="layui-form" id="infForm" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">电话号码</label>
        <div class="layui-input-block">
            <input type="text" name="phoneNumber" placeholder="请输入电话号码" autocomplete="off" class="layui-input"
                   style="width: 300px">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">住址</label>
        <div class="layui-input-block">
            <input type="text" name="address" placeholder="请输入家庭住址" autocomplete="off" class="layui-input"
                   style="width: 300px">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="name" placeholder="请联系人姓名" autocomplete="off" class="layui-input"
                   style="width: 300px">
        </div>
    </div>
</form>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs layui-anim layui-anim-scaleSpring" lay-event="update">更新</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-anim layui-anim-scaleSpring" lay-event="del">删除</a>
</script>

<script type="text/javascript">
    layui.use(['table', 'jquery', 'form', 'layer', 'upload'], function () {
        var table = layui.table;
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;
        var upload = layui.upload;
        table.render({
            elem: '#informationTable'
            , url: '/PersonalInformation/list'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , title: '通讯录信息'
            , cols: [[  //表头
                {type: 'checkbox', fixed: 'left'},
                {field: 'phoneNumber', title: '电话号码'},
                {field: 'address', title: '住址'},
                {field: 'name', title: '姓名'},
                {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
            ]],
            parseData: function (res) {
                console.log(res);
                return {
                    "code": 0,
                    "msg": res.msg,
                    "data": res.data
                }
            }
        });

        table.on('tool(informationTable)', function (obj) {
            var data = obj.data;    //获得当前行数据
            var layEvent = obj.event;   //获得 lay-event 对应的值（也可以是表头的event 参数对应的值）
            var tr = obj.tr;    //获得当前行 tr 的 DOM 对象（如果有的话）

            if (layEvent == 'update') {
                layer.open({
                    title: '更新联系人信息',
                    type: 1,
                    content: $("#infForm"),
                    area: ['500px', '300px'],
                    btn: ['保存'],
                    success: function () {
                        $("#infForm").find("input[name='phoneNumber']").val(data.phoneNumber);
                        $("#infForm").find("input[name='address']").val(data.address);
                        $("#infForm").find("input[name='name']").val(data.name);
                    },
                    btn1: function () {
                        var phone = $("#infForm").find("input[name='phoneNumber']").val();
                        var address = $("#infForm").find("input[name='address']").val();
                        var name = $("#infForm").find("input[name='name']").val();
                        var param = {"phoneNumber": phone, "address": address, "name": name};
                        $.ajax({
                            url: '/PersonalInformation/update',
                            type: 'post',
                            data: param,
                            dataType: 'json',
                            success: function (res) {
                                layer.closeAll('page');
                                layer.msg(res.msg);
                                $("#infForm").find('input').val('');
                                table.reload("informationTable", {});
                            },
                            error: function () {
                                layer.msg("请求失败！");
                            }
                        })
                    }
                })
            } else if (layEvent == 'del') {
                    $.ajax({
                        url: '/PersonalInformation/delete',
                        type: 'post',
                        traditional:true,
                        data: {"phoneNumber":data.phoneNumber},
                        dataType: 'json',
                        success: function (res) {
                            layer.msg(res.msg);
                            if (res.success) {
                                table.reload('informationTable',[]);
                            }
                        }
                    });
            }
        })

        table.on('toolbar(informationTable)',function (obj){
            var data = obj.data;    //获得当前行数据
            var layEvent = obj.event;   //获得 lay-event 对应的值（也可以是表头的event 参数对应的值）
            var tr = obj.tr;    //获得当前行 tr 的 DOM 对象（如果有的话）

            if (layEvent === 'add') {
                layer.open({
                    title: '添加联系人信息',
                    type: 1,
                    content: $("#infForm"),
                    area: ['500px', '300px'],
                    btn: ['保存'],
                    success: function () {
                        $("#infForm").find('input').val('');
                        // $("#infForm").find("input[name='phoneNumber']").val(data.phoneNumber);
                        // $("#infForm").find("input[name='address']").val(data.address);
                        // $("#infForm").find("input[name='name']").val(data.name);
                    },
                    btn1: function () {
                        var phone = $("#infForm").find("input[name='phoneNumber']").val();
                        var address = $("#infForm").find("input[name='address']").val();
                        var name = $("#infForm").find("input[name='name']").val();
                        var param = {"phoneNumber": phone, "address": address, "name": name};
                        $.ajax({
                            url: '/PersonalInformation/add',
                            type: 'post',
                            data: param,
                            dataType: 'json',
                            success: function (res) {
                                layer.closeAll('page');
                                layer.msg(res.msg);
                                $("#infForm").find('input').val('');
                                table.reload("informationTable", {});
                            },
                            error: function () {
                                layer.msg("请求失败！");
                            }
                        })
                    }
                })
            } else if (layEvent == 'update') {
                layer.open({
                    title: '添加联系人信息',
                    type: 1,
                    content: $("#infForm"),
                    area: ['500px', '300px'],
                    btn: ['保存'],
                    success: function () {
                        $("#infForm").find("input[name='phoneNumber']").val(data.phoneNumber);
                        $("#infForm").find("input[name='address']").val(data.address);
                        $("#infForm").find("input[name='name']").val(data.name);
                    },
                    btn1: function () {
                        var phone = $("#infForm").find("input[name='phoneNumber']").val();
                        var address = $("#infForm").find("input[name='address']").val();
                        var name = $("#infForm").find("input[name='name']").val();
                        var param = {"phoneNumber": phone, "address": address, "name": name};
                        $.ajax({
                            url: '/PersonalInformation/update',
                            type: 'post',
                            data: param,
                            dataType: 'json',
                            success: function (res) {
                                layer.closeAll('page');
                                layer.msg(res.msg);
                                $("#infForm").find('input').val('');
                                table.reload("informationTable", {});
                            },
                            error: function () {
                                layer.msg("请求失败！");
                            }
                        })
                    }
                })
            }
        })
    })
</script>

</body>
</html>